package engine.logic
{
	import engine.data.ModelLocator;
	import engine.data.SceneObject;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	
	import newx.utils.coders.MD5;
	import newx.utils.debug.IO;
	
	import toolNet.Model;

	public class ActSee
	{
		private var isConfig:Boolean;
		/**服务器ID*/	
		public static var s_serverId	:String = "0";
		/**服务器IP*/
		public static var s_serverIp	:String = "";
		/**账号*/
		public static var s_account	:String = "";
		/**角色名*/
		public static var s_roleName	:String = "0";
		
		private static var sendObjVo:ActVo	=	new ActVo();
		
		private static var STATUS:String		=	"STATUS";
		private static var OPEN_UI:String		=	"OPEN_UI";
		private static var CLICK_BTN:String	=	"CLICK_BTN"
		
		/**开启日志功能*/
		private static var isOpen:Boolean		=	false;
		
		public function ActSee()
		{
		}
		
		/**
		 * 发送日志 				登陆类型的
		 * @param act_id		事件ID
		 * @param act_result	事件结果（0/1） 0表示失败，1表示成功
		 * @param act_time		事件花销时间
		 * @see 日志查看	http://192.168.2.51:82/
		 */		
		public static function PostClientStatus(act_id : int, act_result : int, act_time : int,send : Boolean = false) : void
		{
			if(!isOpen)
				return;
			sendObjVo.id		=	act_id;
			sendObjVo.result	=	act_result;
			sendObjVo.time		=	act_time
			PostLogSend(sendObjVo,STATUS,send);	
		}
		
		/**
		 * 发送日志             不用了
		 * @param act_id		事件ID
		 * @param act_result	事件结果（0/1） 0表示失败，1表示成功
		 * @param act_time		事件花销时间
		 * @see 日志查看	http://192.168.2.51:82/
		 */	
		public static function PostLoginStatus(act_id : int, act_result : int, act_time : int) : void
		{
			if(!isOpen)
				return;
			
			IO.traceLog("---------- 客户端   id = " + act_id + "  result = " + act_result + "  time = " + act_time);
			sendObjVo.id	=	act_id;
			sendObjVo.result = act_result;
			sendObjVo.time	=	act_time
//			PostLogSend(sendObjVo,STATUS,true);
		}
		/**
		 * 按钮类型的 
		 * @param act_id
		 * @param clickNum
		 * 
		 */		
		public static function PostLoginBtn(act_id : int,clickNum:int):void
		{
			if(!isOpen)
				return;
			
			sendObjVo.id 		= act_id;
			sendObjVo.num		=	clickNum;
			PostLogSend(sendObjVo,CLICK_BTN,true);
		}
		/**
		 *  
		 * @param act_id
		 * @param act_num
		 * @param act_target
		 * @param act_time
		 * 
		 */		
		public static function PostLogicUI(act_id : int, act_num : int, act_target : String, act_time : int):void
		{
			if(!isOpen)
				return;
			
			sendObjVo.id 			= 	act_id;
			sendObjVo.num			=	act_num;
			sendObjVo.targetName	=	act_target;
			sendObjVo.time			=	act_time
			PostLogSend(sendObjVo,OPEN_UI,true);
		}
		/**
		 *  发送日志
		 * @param sendVo 对象
		 * @param type   类型 
		 * @param send   是否发送
		 * 
		 */		
		private static function PostLogSend(sendVo:ActVo,type:String,send:Boolean =	false):void
		{
			//act_id : int, act_result : int, act_time : int,
			//第一次去角色名
			var role : SceneObject = ModelLocator.getInstance().scene.Role;
			if(role !=null){
				
				if(ModelLocator.getInstance().scene.Role.CheckProperties("Name"))
				{
					s_roleName = ModelLocator.getInstance().scene.Role.GetPropertiesEx("Name");
					Model.getInstance().connect(s_roleName);
				}
			}
			if(s_roleName == "" && !send)return;
			
			var s_post		:String	=	ModelLocator.getInstance().mainIni.getContent("Log","LogIp");
			var game_id		:String	=	ModelLocator.getInstance().mainIni.getContent("Log","Game_id");
			var	s_webIp		:String	=	ModelLocator.getInstance().mainIni.getContent("Log","s_webIp");
			
			if (s_post != null && s_post.length > 0)
			{
				var act_idStr : String = sendVo.id.toString();
				var count : int = 6 - act_idStr.length;
				while (count > 0)
				{
					act_idStr = "0" + act_idStr;
					--count;
				}
				var request		: URLRequest 	= new URLRequest(s_post);
				var requestVars	: URLVariables 	= new URLVariables;
				requestVars.game_id = game_id;
				requestVars.server_id = s_serverId;
				
				requestVars.act_id = act_idStr;
				
				if(type == STATUS)
				{
					requestVars.act_result = sendVo.result;
					requestVars.act_time = sendVo.time;
				}
				else if(type == OPEN_UI)
				{
					requestVars.extend_data		=	sendVo.num
					requestVars.act_name	=	sendVo.targetName;
					requestVars.act_time 	= sendVo.time;
				}
				else if(type == CLICK_BTN)
				{
					requestVars.extend_data	=	sendVo.num
				}
//				requestVars.act_id = act_idStr;
//				requestVars.act_result = sendVo.result;
//				requestVars.act_time = sendVo.time;
				
				
				requestVars.acount = s_account;
				requestVars.role_name = s_roleName;
				requestVars.c_ip = "0.0.0.0";
				requestVars.c_type = 2;
				
				var str 	: String = game_id + s_serverId + act_idStr + s_account;
				//var str 	: String = game_id + s_serverId + act_idStr + s_account + "KGGT25@Sn&202";
			//	var v_key 	: String = MD5.hash(str);
				
				//requestVars.v_key 		= v_key.toUpperCase();
				requestVars.server_ip 	= s_serverIp;
				requestVars.web_ip 		= s_webIp;
				
				request.data 			= requestVars;
				request.method 			= URLRequestMethod.POST;
				
				var loader : URLLoader = new URLLoader;
				loader.dataFormat = URLLoaderDataFormat.TEXT;
				loader.addEventListener(IOErrorEvent.IO_ERROR, OnPostClientStatusOver, false, 0, true);
				loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, OnPostClientStatusOver, false, 0, true);
				loader.addEventListener(Event.COMPLETE, OnPostClientStatusOver, false, 0, true);
				try
				{
					loader.load(request);
				}
				catch (e : Error)
				{
					loader.removeEventListener(IOErrorEvent.IO_ERROR, OnPostClientStatusOver, false);
					loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, OnPostClientStatusOver, false);
					loader.removeEventListener(Event.COMPLETE, OnPostClientStatusOver, false);
				}
			}
		}
		private static function OnPostClientStatusOver(event : Event) : void
		{
			var loader : URLLoader = event.currentTarget as URLLoader;
			loader.removeEventListener(IOErrorEvent.IO_ERROR, OnPostClientStatusOver, false);
			loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, OnPostClientStatusOver, false);
			loader.removeEventListener(Event.COMPLETE, OnPostClientStatusOver, false);
			try{
				loader.close();
			}catch (e : Error) { }
		}
	}
}